<?doctype html?>
<html>
<header>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>AkkaWamp :: HelloClient</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
  <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
  <![endif]-->
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/default.min.css">
</header>
<body>
  <h1>AkkaWamp :: HelloClient</h1>
  <p>Click on of the following tabs to have a look at relevant Scala code fragments</p>
  <div>
    <!-- Nav tabs -->
    <ul class="nav nav-tabs" role="tablist">
      <li role="presentation" class="active">
        <a href="#publish" aria-controls="publish" role="tab" data-toggle="tab">
          <h2>publish</h2>
        </a>
      </li>
      
      <li role="presentation">
        <a href="#subscribe" aria-controls="subscribe" role="tab" data-toggle="tab">
          <h2>subscribe</h2>
        </a>
      </li>
      
      <li role="presentation">
        <a href="#register" aria-controls="register" role="tab" data-toggle="tab">
          <h2>register</h2>
        </a></li>
      
      <li role="presentation">
        <a href="#call" aria-controls="call" role="tab" data-toggle="tab">
          <h2>call</h2>
        </a>
      </li>
    </ul>

    <!-- Tab panes -->
    <div class="tab-content">
      <div role="tabpanel" class="tab-pane active" id="publish">
        <pre><code class="scala">val publication = for {
  session <- client.
    open(
      url = "ws://localhost:8080/ws",
      realm = "hello.realm")
  publication <- session.
    tickle(
      topic = "hello.counter", 
      interval = 1 second)
}
yield tickling
        </code></pre>
        <p>AutobahnJS peer has just received event #<code id="counter" class="highlight">0</code> from AkkaWamp peer which is repeatedly publishing a counter tick.</p>
        <p>NOTE: AkkaWamp <code>Session.tickle()</code> is an utility method which repeatedly invokes <code>Session.publish()</code> to publish a <i>tick count</i> event to the given topic.</p>
      </div>
      
      <div role="tabpanel" class="tab-pane" id="subscribe">
        <pre><code class="scala">val subscription = for {
  session <- client.
    open(
      url = "ws://localhost:8080/ws",
      realm = "hello.realm")
  subscription <- session.
    subscribe(
      topic = "hello.greeting") { 
      event => 
        event.data.map(println)}
} 
yield subscription
        </code></pre>
        <p>AutobahnJS peer is repeatedly publishing <code>Ciao!</code> to <code>hello.greeting</code> and AkkaWamp peer is just printing it to its output console.</p>
      </div>
      
      <div role="tabpanel" class="tab-pane" id="register">
        <pre><code class="scala">val registration = for {
  session <- client.
    open(
      url = "ws://localhost:8080/ws",
      realm = "hello.realm")
  registraton <- session.
    register(
      procedure = "hello.sum", 
      impl = (a: Int, b: Int) => a + b)
}
yield registration
        </code></pre>
        <p>AutobahnJS peer is repeatedly invoking <code>hello.sum(</code><code id="x">x</code><code>, 3)</code> and receiving <code id="sum">0</code> as result from AkkaWamp peer.</p>
      </div>

      <div role="tabpanel" class="tab-pane" id="call">
        <pre><code class="scala">val result = for {
  session <- client.
    open(
      url = "ws://localhost:8080/ws",
      realm = "hello.realm")
  result <- session.
    call(
      procedure = "hello.multiply",
      args = List(12, 67))
} 
yield result
        </code></pre>
      </div>
    </div>
  </div>

  <h2>Full Scala code</h2>
  <p>Following is the full implementation in Scala</p>
  <pre><code class="scala">
object HelloClient extends App {
  import scala.concurrent.duration._
  import akka.wamp.client._
  
  val client = Client()
  implicit val ec = client.executionContext
  
  for {
    session <- client.
      open(
        url = "ws://localhost:8080/ws",
        realm = "hello.realm")
    publication <- session.
      tickle(
        topic = "hello.counter",
        interval = 1 second)
    subscription <- session.
      subscribe(
        topic = "hello.greeting",
        (event) => event.data.map(println))
    registration <- session.
      register(
        procedure = "hello.sum", 
        handler = (a: Int, b: Int) => a + b)
    result <- session.
      call(
        procedure = "hello.multiply",
        args = List(54, 54)
      )
  } 
  yield ()
}
  </code></pre>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/languages/scala.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script>
  
  <script src="http://autobahn.s3.amazonaws.com/autobahnjs/latest/autobahn.min.jgz"></script>
  <script src="client.js"></script>
</body>
</html>